<template>

	<view class="addressContainer">
		<!-- 空白页面 -->
		<view class="empty" v-if='adressList===null||adressList.length===0'>
			<text>暂未添加收货地址</text>
			<text>请点击下方添加按钮</text>
		</view>


		<!-- 展示已有页面 -->
		<view class="addressList">
			<view class="addressItem" v-for="(address, index) in adressList" :key="index">
				<view class="personInfo">
					<view class="textContent">
						<view class="name">{{ address.addressConsigneeName }}</view>
						<view class="phone">{{ address.addressConsigneePhone }}</view>
						<view class="default" v-if="address.addressWxuserDefault==1">默认</view>
					</view>


					<view class="iconConent">
						<uni-icons type="trash" size="22" style="margin-right: 40rpx;"
							@click="deleteAddress(address.id)"></uni-icons>
						<uni-icons type="compose" size="22" @click="editAddress(address.id)"></uni-icons>
					</view>
				</view>
				<view class="addressInfo" @click="changeOrderAddress(address.id)">
					<text>{{ address.addressConsigneeProvinceName }}{{ address.addressConsigneeCityName }}{{ address.addressConsigneeDistrictName }}{{ address.addressConsigneeDetail }}</text>
				</view>
			</view>
		</view>



		<!-- 底部地址操作区域 -->
		<view class="addressFooter">
			<view class="addressContent">
				<button class="button" type="submit" @click="addOrEdit">添加收货地址</button>
			</view>
		</view>
		<!-- 自己封装的弹出框 -->
		<confirmPop :isPopUp="confirmStatus==1" title="确认删除地址吗" @cancel="cancel()" @confirm="confirmDel()">
		</confirmPop>
		<!-- 遮罩层 -->
		<view class="mask" v-if="confirmStatus==1"></view>
		<!-- 级联选择器 -->
		<!-- 省市区选择 province city area初始省市区设置 show:是否显示  @changeClick：更改省市区事件 @sureSelectArea：确认事件 @hideShow：隐藏事件-->
		<cc-selectDity :province="addressData.addressConsigneeProvinceName" :city="addressData.addressConsigneeCityName"
			:area="addressData.addressConsigneeDistrictName" :show="show" @changeClick="changeClick"
			@sureSelectArea="onsetCity" @hideShow="onhideShow"></cc-selectDity>
	</view>

</template>

<script>
	import * as AddressApi from '@/api/address/address.js'
	import confirmPop from '@/components/confirmPop.vue'
	export default {
		components: {
			confirmPop
		},
		data() {
			return {
				//表示点击地址item是否触发修改订单地址函数
				isChooseStatus: false,
				//到时候通过 adressList的length来控制页面的显示
				adressList: [],
				//是编辑还是增加 0表示新增 1表示更新
				saveStatus: 0,
				//用来提示用户正确填写表单
				errMsg: '',
				addressData: {
					addressConsigneeName: '',
					addressConsigneePhone: '',
					addressConsigneeProvinceName: '',
					addressConsigneeCityName: '',
					addressConsigneeDistrictName: '',
					addressConsigneeDetail: '',
					addressWxuserDefault: 0
				},
				// 用来放省市县 
				address: '',
				// 级联选择器配置
				colors: '#f23a3a',
				show: false,
				//用来临时存需要删除的地址 id
				delId: 0,
				// 遮罩层状态
				confirmStatus: 0
			};
		},

		onLoad(options) {
			// 初始化地址列表
			const paramsStr = options.params || null; // 获取URL查询参数中的params字段
			//接收个人中心传来的订单类型
			if (paramsStr != null) {
				const receivedParams = JSON.parse(paramsStr);
				if (receivedParams.isChooseStatus === true) {
					this.isChooseStatus = true;
				}

			}

			this.addressInit();

		},
		onShow() {
			this.addressInit();
		},
		methods: {
			//初始化地址列表
			async addressInit() {
				const res = await AddressApi.getAddressList();

				if (res.data.code === 200) {
					this.adressList = res.data.data;
				}


			},

			// 切换页面状态
			addOrEdit() {
				uni.$toPage('/subpkg/shoppingMall/editAddress/editAddress', {
					saveStatus: 0
				})


			},
			// 表单校验函数
			validateBeforeSave() {

				if (!this.addressData.addressConsigneeName) {
					this.errMsg = "请填写收货人姓名";
					return false;
				}

				if (!this.addressData.addressConsigneePhone) {
					this.errMsg = "请填写收货人手机号";
					return false;
				}

				if (!this.address) {
					this.errMsg = "所在地区不能为空";
					return false;

				}

				if (this.addressData.addressConsigneeDetail == "") {
					this.errMsg = "详细地址不能为空";
					return false;
				}
				return true;
			},

			// 编辑地址
			async editAddress(id) {

				uni.$toPage('/subpkg/shoppingMall/editAddress/editAddress', {
					saveStatus: 1,
					id: id
				})

			},


			// 获取文本框的值
			handleInput(e) {

			},

			//规范用户输入的方法
			checkPhone(e) {
				const phoneNumber = e.target.value.trim();
				const reg = /^(\+86)?1[3-9]\d{9}$/; // 匹配+86开头和不带+86的手机号  
				if (!reg.test(phoneNumber)) {
					// 手机号格式不正确，进行相应处理  
					uni.$showMsg("请输入合法手机号");
					this.addressData.addressConsigneePhone ='';
				}
			},

			// 触发删除确认框
			deleteAddress(id) {
				this.confirmStatus = 1;
				this.delId = id;


			},


			async confirmDel() {
				this.confirmStatus = 0;
				const res = await AddressApi.deleteAddress(this.delId);
				this.addressInit()
				const code = res.data.code;
				if (code == 200) {
					uni.$showMsg("删除成功")
					this.$globalData.commonDeleteFlag = true;
				}


			},

			// 关闭确认框
			cancel() {
				this.confirmStatus = 0;
			},



			// 级联选择器的方法
			openPicker() {

				this.show = true
			},

			changeClick(value, value2, value3) {


				this.addressData.addressConsigneeProvinceName = value;
				this.addressData.addressConsigneeCityName = value2;
				this.addressData.addressConsigneeDistrictName = value3;

			},
			onhideShow() {
				this.show = false

			},
			//选中省市区
			onsetCity(e) {
				let data = e.detail.target.dataset;
				let address = data.province + data.city + data.area;
				this.show = false
				this.address = address

			},
			// 
			switchChange(e) {

			},

			//设置默认地址的方法
			setDefault() {
				uni.$showMsg("修改默认地址成功");
				if (this.addressData.addressWxuserDefault == 0) this.addressData.addressWxuserDefault = 1;
				else this.addressData.addressWxuserDefault = 0;
			},

			//当订单向这边传输信息时，切换状态，切换地址生效
			changeOrderAddress(addressId) {
				if (this.isChooseStatus === false) {
					return;
				} else {
					this.$globalData.selectedCommonAddress = addressId;
					uni.$goBack();
				}


			}




		}


	}
</script>

<style lang="scss" scoped>
	* {
		box-sizing: border-box;
	}

	.addressContainer {

		display: flex;
		justify-content: center;
		box-sizing: border-box;
		background: rgba(243, 243, 245, 1);
		position: relative;


		//自定义遮罩层
		.mask {
			background: rgba(0, 0, 0, 0.25);
			width: 100vw;
			height: 100vh;
			position: absolute;
		}

		//空白页
		.empty {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			position: absolute;
			top: 450rpx;
			font-size: 40rpx;
			color: rgba(102, 102, 102, 1);
			width: 360rpx;
			height: 116rpx;
		}




		// 地址展示页面
		.addressList {
			box-sizing: border-box;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 34rpx;

			.addressItem {
				box-sizing: border-box;
				display: flex;
				flex-direction: column;
				padding: 22rpx;
				width: 692rpx;
				border-radius: 14rpx;
				margin-bottom: 22rpx;
				background-color: rgb(255, 255, 255);


				.personInfo {
					display: flex;
					justify-content: space-between;
					margin-bottom: 20rpx;

					.textContent {
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: center;
						position: relative;
						font-size: 28rpx;
						color: rgba(85, 85, 87, 1);


						.name {
							height: 42rpx;
							line-height: 42rpx;
							box-sizing: border-box;
							flex-wrap: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;



						}

						.phone {

							height: 42rpx;
							margin-left: 5rpx;
							line-height: 42rpx;
							box-sizing: border-box;

						}

						.default {

							font-size: 16rpx;
							color: rgba(243, 243, 245, 1);
							background-color: rgba(179, 130, 75, 1);
							width: 64rpx;
							height: 24rpx;
							border-radius: 6rpx;
							margin-left: 20rpx;
							box-sizing: border-box;
							display: flex;
							justify-content: center;
							align-items: center;



						}

					}



				}

				.addressInfo {
					overflow: hidden;
					white-space: normal;
					box-sizing: border-box;
					width: 645rpx;
					color: rgba(0, 0, 0, 1);
					font-size: 32rpx;
					font-weight: 500;

				}


			}

		}

		// 地址编辑页面
		.addressEdit {

			.addressDetailBox {
				border-radius: 14rpx;
				margin-top: 34rpx;
				background: rgba(255, 255, 255, 1);
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;
				width: 692rpx;
				height: 518rpx;
				box-sizing: border-box;
				// padding-bottom: 100rpx;

				view {

					height: 116rpx;
					width: 100%;
					display: flex;
					flex-direction: row;
					align-items: flex-end;

					.underLine {
						padding-bottom: 10rpx;
						border-bottom: solid;
						border-bottom: 1rpx solid rgba(229, 229, 229, 1);
						margin: 0 30rpx;
						position: relative;

						.text {
							font-weight: 400;
							color: rgba(0, 0, 0, 1);
							font-size: 32rpx;
						}

						input {
							font-size: 28rpx;
							font-weight: 400;
							color: rgba(30, 30, 32, 1);
							position: absolute;
							top: 70rpx;
							left: 160rpx;
						}

					}
				}


				.areaDetail {
					height: 193rpx;

					.underLine {

						position: relative;

						.text {

							position: absolute;
							top: -12rpx;
							height: 50rpx;
							margin-top: 0rpx;

							.textarea {
								position: absolute;
								top: 10rpx;
								left: 135rpx;
								font-size: 28rpx;
								font-weight: 400;
							}

						}


					}

				}
			}


			// 默认地址
			.default {
				border: solid;
				display: flex;
				align-items: center;
				box-sizing: border-box;

				.icon-content {
					width: 48rpx;
					margin-right: 0rpx;
				}

				.title {
					margin: 20rpx 10rpx;
					margin-right: 26rpx;
					font-size: 28rpx;
					color: rgba(0, 0, 0, 1);
				}

				.title-detail {
					font-size: 24rpx;
					color: rgba(167, 167, 167, 1);
				}

			}


		}


		// 底部操作区域
		.addressFooter {
			display: flex;
			width: 100%;
			height: 190rpx;
			position: fixed;
			bottom: 0;
			background: rgba(255, 255, 255, 1);

			.addressContent {
				margin-top: 14rpx;
				display: flex;
				width: 100%;

				.button {
					font-size: 28rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 406rpx;
					height: 74rpx;
					line-height: 74rpx;
					border-radius: 26rpx;
					background: rgba(223, 161, 60, 1);
					padding: 16rpx;
					font-weight: 500;
					color: rgba(255, 255, 255, 1);
				}

			}
		}


	}
</style>